package ppjava12v1.chess.entities.games;

import ppjava12v1.chess.desk.Desk;
import ppjava12v1.chess.entities.users.User;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * Класс описывающий партию:
 * - игроки
 * - цвета (кто каким цветом играет)
 * - Дата начала
 * - Дата завершения
 * - победитель (если отсутсвует - значит ничья)
 */
@Entity
@Table(name = "games")
public class Game implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name = "white", foreignKey = @ForeignKey(name = "fk_wplayer"))
    private User whitePlayer;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name = "black", foreignKey = @ForeignKey(name = "fk_bplayer"))
    private User blackPlayer;

    @Column(name = "start_game")
    private Date dateStartGame;

    @Column(name = "end_game")
    private Date dateEndGame;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(nullable = true, foreignKey = @ForeignKey(name = "fk_winner"))
    private User winner;

    @Transient
    private Desk desk;

    public Game() {
        desk = new Desk();
    }

    public Desk getDesk() {

        if (desk == null)
            desk = new Desk();

        return desk;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public User getWhitePlayer() {
        return whitePlayer;
    }

    public void setWhitePlayer(User whitePlayer) {
        this.whitePlayer = whitePlayer;
    }

    public User getBlackPlayer() {
        return blackPlayer;
    }

    public void setBlackPlayer(User blackPlayer) {
        this.blackPlayer = blackPlayer;
    }

    public Date getDateStartGame() {
        return dateStartGame;
    }

    public void setDateStartGame(Date dateStartGame) {
        this.dateStartGame = dateStartGame;
    }

    public Date getDateEndGame() {
        return dateEndGame;
    }

    public void setDateEndGame(Date dateEndGame) {
        this.dateEndGame = dateEndGame;
    }

    public User getWinner() {
        return winner;
    }

    public void setWinner(User winner) {
        this.winner = winner;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Game)) return false;

        Game game = (Game) o;

        if (id != game.id) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (int) (id ^ (id >>> 32));
    }
}
